using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Configuration;


namespace InstaladorWebService
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            }


        private void AlterConfigFile(string sDir)
        {
            XmlTextReader reader = new XmlTextReader(sDir);
            XmlDocument doc = new XmlDocument();
            doc.Load(reader);
            reader.Close();
            XmlNode Nodes = doc.SelectSingleNode("//MSU_WebServices.Properties.Settings");            
            Nodes.InnerXml = Nodes.InnerXml.Replace("http://localhost:8080/moodle/", textBox3.Text+@"/");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            try
            {   
                if(textBox1.Text.Trim()==null){
                    MessageBox.Show("Complete Port Value");
                    button1.Enabled = true;
                    return;
                }
                if (textBox3.Text.Trim() == null)
                {
                    MessageBox.Show("Complete Moodle Url");
                    button1.Enabled = true;
                    return;
                }

                string cur_dir = Directory.GetCurrentDirectory();
                AlterConfigFile(cur_dir + @"\web.config");  

                string sDir = @"C:\\Inetpub\\wwwroot\\MSU_WebService\";

                //ExecuteFile("%windir%\\Microsoft.NET\\Framework\\v2.0.50727\\aspnet_regiis", "-sn W3SVC/");
                ExecuteFile(@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis", "-sn W3SVC/");

                System.IO.Directory.CreateDirectory(sDir);
                string path = Directory.GetCurrentDirectory();
                File.Copy(path + @"\Login.asmx", sDir + "Login.asmx", true);

                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(path + @"\Web.config");
                XmlNodeList name = xDoc.GetElementsByTagName("value");
                name[0].InnerText = textBox3.Text+@"/wspp/service_pp.php";
                xDoc.Save(path + @"\Web.config");                

                File.Copy(path + @"\Web.config", sDir + "Web.config", true);
                Directory.CreateDirectory(sDir + @"\bin\");
                DirectoryCopy(path + @"\bin\", sDir + @"\bin\");

                ExecuteFile(@"C:\WINDOWS\system32\iisweb.vbs", "/create \"C:\\Inetpub\\wwwroot\\MSU_WebService\" msuwebservice /b " + textBox1.Text);
            }
            catch (Exception error)
            {
                MessageBox.Show("Error on installing");
                button1.Enabled = true;
                return;
            }
            MessageBox.Show("WebService install completed \n Installer will be close");

            Application.Exit();            
        }

        public static void DirectoryCopy(string sourcePath, string destinationPath)
        {
            DirectoryInfo sourceDirectory = new DirectoryInfo(sourcePath);
            DirectoryInfo destinationDirectory = new DirectoryInfo(destinationPath);
            if (destinationDirectory.Exists == false)
            {
                destinationDirectory.Create();
                destinationDirectory.Attributes = sourceDirectory.Attributes;
            }

            foreach (FileInfo fileInfo in sourceDirectory.GetFiles())
            {
                fileInfo.CopyTo(destinationDirectory.FullName + @"\" + fileInfo.Name, true);
            }

            foreach (System.IO.DirectoryInfo directoryInfo in sourceDirectory.GetDirectories())
            {
                DirectoryCopy(directoryInfo.FullName, destinationDirectory.FullName + @"\" + directoryInfo.Name);
            }
        }

        static void ExecuteFile(string FileName, string arguments)
        {
            System.Diagnostics.Process p = System.Diagnostics.Process.Start(FileName, arguments);
            p.WaitForExit();
            //return;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult select_dir = folderBrowserDialog1.ShowDialog();
            textBox3.Text = folderBrowserDialog1.SelectedPath;
        }

        private void button2_Click_1(object sender, EventArgs e)
        {

        }

    }
}